package com.example.demo_mysql_mybatisplus.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.net.NetUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.boot.info.BuildProperties;
import org.springframework.boot.info.GitProperties;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class DeployInfoUtil implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        DeployInfoUtil.context = context;
    }

    /** 因为jenkins导致仓库游离而注释，换用jenkins主动写文件:gitProperties；因此相关的功能在没有主动写文件时是会受影响的 **/

    /** 获取jar程序部署的版本等基础信息
     * */
    public static JSONObject get() {
        JSONObject json = null;
        try {
            GitProperties gitProperties = (GitProperties) DeployInfoUtil.context.getBean("gitProperties");
            BuildProperties buildProperties = (BuildProperties) DeployInfoUtil.context.getBean("buildProperties");

            // git 信息
            Map<String,String> gitPropertiesMap = new HashMap<>( );
            gitProperties.forEach(entry->{
                gitPropertiesMap.put(entry.getKey(),entry.getValue());
            });
            JSONObject gitJson = JSONUtil.parseObj(gitPropertiesMap);
            // mvn构建 信息
            Map<String,String> buildPropertiesMap = new HashMap<>( );
            buildProperties.forEach(entry->{
                if("time".equals(entry.getKey())){
                    buildPropertiesMap.put(entry.getKey(), DateUtil.formatDateTime( Date.from(buildProperties.getTime()) ));
                }else{
                    buildPropertiesMap.put(entry.getKey(),entry.getValue());
                }
            });
            if ( "" instanceof String ){

            }
            JSONObject buildJson = JSONUtil.parseObj(buildPropertiesMap);
            // 本地 信息
            JSONObject localInfo = JSONUtil.createObj();
            localInfo.put("ip", NetUtil.getLocalhostStr());
            localInfo.put("host", NetUtil.getLocalhost()==null?null:NetUtil.getLocalhost().getHostName());
            localInfo.put("mac", NetUtil.getLocalMacAddress());
            localInfo.put("jarName",System.getProperty("java.class.path"));
            // jar 路径
            ApplicationHome home = new ApplicationHome(DeployInfoUtil.class);
            File jarFile = home.getSource();
            if(jarFile!=null){
                localInfo.put("jarPath",jarFile.getParentFile().toString());
            }else{
                localInfo.put("jarPath", DeployInfoUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath());
            }
            // 组装
            json = JSONUtil.createObj();
            json.put("gitProperties",gitJson);
            json.put("buildProperties",buildJson);
            json.put("localInfo",localInfo);

            log.warn("部署信息:"+json.toString());
        } catch (BeansException e) {
            log.error("获取部署信息异常:" + e.getMessage());
        }
        return json ;
    }


}
